<?php

class ItemController extends Controller
{
	/**
	 * @var string the default layout for the views.
	 */
	public $layout='//layouts/column1';

	/**
	 * Initializes the controller.
	 */
	public function init()
	{
		$this->defaultAction='catalog';
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('view'),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array('catalog'),
				'users'=>array('@'),
			),
			array('allow',
				'actions'=>array('create','update','admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$item=$this->loadModel($id);
		$model=new ReservationForm;

		if(isset($_POST['ReservationForm']))
		{
			$model->attributes=$_POST['ReservationForm'];
			if($model->validate())
				$this->redirect(array('//reservation/create','itemId'=>$model->itemId,'quantity'=>$model->quantity));
		}

		if($id!==null)
			$model->itemId=$id;

		$this->render('view',array(
			'item'=>$item,
			'model'=>$model,
		));
	}

	/**
	 * Displays the item catalog.
	 */
	public function actionCatalog()
	{
		$dataProvider=new CActiveDataProvider('Item',array(
			'criteria'=>array('condition'=>'deleted=0'),
			'pagination'=>array('pageSize'=>100),
		));

		$models=array();
		if($dataProvider->getData()!==array())
		{
			$data=$dataProvider->getData();
			foreach($data as $item)
			{
				$model=new ReservationForm();
				$model->itemId=$item->id;
				$model->quantity=1;
				$models[$item->id]=$model;
			}
		}

		if(isset($_POST['ReservationForm']))
		{
			$model=$models[$_POST['ReservationForm']['itemId']];
			$model->attributes=$_POST['ReservationForm'];
			if($model->validate())
				$this->redirect(array('//reservation/create','itemId'=>$model->itemId,'quantity'=>$model->quantity,'#'=>null));
		}

		$this->render('catalog',array(
			'dataProvider'=>$dataProvider,
			'models'=>$models,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Item;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Item']))
		{
			$model->attributes=$_POST['Item'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Item']))
		{
			$model->attributes=$_POST['Item'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Item('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Item']))
			$model->attributes=$_GET['Item'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 * @return Item
	 */
	public function loadModel($id)
	{
		$model=Item::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='item-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
